import {Utils} from '../utils/Utils';
import {AjaxMsg} from '../utils/AjaxMsg';

export class SendSmsButton {
    constructor(form, dom){
        this.form = form;
        this.app = form.app;
        this.api = form.app.api;
        this.dom = dom;

        this._defaultLabel = this.dom.text();
        this._disabled = false;
        this._countdownTimer = null;

        this.dom.on('click', this.onClick.bind(this));
    }

    disable(){
        this.dom.prop('disabled', true);
        this._disabled = true;
    }

    enable(){
        this.dom.prop('disabled', false);
        this._disabled = false;
    }

    onChangeMobileNum(){
        if(this._disabled){
            this.enable();
            this.dom.text(this._defaultLabel);
        }
        if(this._countdownTimer){
            this.stopCountdown();
        }
    }

    startCountdown(countdownTime, serverTime = false){
        let offset = 0;
        let time = Utils.getTime();
        this.stopCountdown(true);
        if(serverTime && Math.abs(serverTime - time) > 5){ //如果服务器时间和本地相差大于5秒，就以服务器时间为准
            offset = serverTime - time;
        }
        let countdown = countdownTime - (time + offset);
        let lastTime = countdown;
        this.disable();
        this.dom.text(countdown.toFixed(0) + ' 秒后可重发');
        this._countdownTimer = setInterval(() => {
            let time = Utils.getTime();
            if(time >= countdownTime){
                this.stopCountdown();
            } else {
                let countdown = countdownTime - (Utils.getTime() + offset);
                if(countdown !== lastTime){
                    this.dom.text(countdown.toFixed(0) + ' 秒后可重发');
                    lastTime = countdown;
                }
            }
        }, 100);
    }

    stopCountdown(onlyTimer = false){
        if(this._countdownTimer){
            clearInterval(this._countdownTimer);
            this._countdownTimer = null;
            if(!onlyTimer){
                this.enable();
                this.dom.text(this._defaultLabel);
            }
        }
    }

    onClick(){
        if(this.form.mobileNumInput.validate()){ //先检验一下手机号格式对不对
            //显示验证码
            this.app.captcha.verify().then((validateData) => {
                let data = Utils.mergeObject(this.form.getFormData(), validateData, true);
                this.api.sendSmsCode(data).then((msg) => {
                    let ajaxMsg = new AjaxMsg(msg);
                    ajaxMsg.showFocus(this.form);
                    ajaxMsg.showMsg();
                    if(ajaxMsg.status > 0){ //发送成功
                        //清空上次输入的验证码，虽然应该没人蠢到验证码对了还重发，总之避免一下逻辑错误
                        this.form.smsVerifyCodeInput.clear();
                        this.startCountdown(ajaxMsg.data.countdown, ajaxMsg.data.time);
                    } else {
                        this.enable();
                        this.dom.text(this._defaultLabel);
                        if(ajaxMsg.status === -1){ //如果没有注册就禁止登录
                            this.disable();
                            //this.dom.text('请注册账号');
                        }
                    }
                });
            });
        }
    }
}